<% import grails.persistence.Event %>
<%=packageName ? "package ${packageName}\n\n" : ''%>class ${className}Controller {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

   

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [${propertyName}List: filterService.filter( params, ${className} ), ${propertyName}Total: filterService.count( params,  ${className} ) ,filterParams: com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params)]
    }

   def updateList = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [${propertyName}List: filterService.filter( params, ${className} ), ${propertyName}Total: filterService.count( params,  ${className} ) ,filterParams: com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params)]

    }

    def create = {
        def ${propertyName} = new ${className}()
        ${propertyName}.properties = params
        return [${propertyName}: ${propertyName}]
    }

    def save = {
        def ${propertyName} = new ${className}(params)
        if (${propertyName}.save(flush: true)) {
            flash.message = "\${message(code: 'default.created.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), ${propertyName}.id])}"
            redirect(action: "show", id: ${propertyName}.id)
        }
        else {
            render(view: "create", model: [${propertyName}: ${propertyName}])
        }
    }

    def show = {
        def ${propertyName} = ${className}.get(params.id)
        if (!${propertyName}) {
            flash.message = "\${message(code: 'default.not.found.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), params.id])}"
            redirect(action: "list")
        }
        else {
            [${propertyName}: ${propertyName}]
        }
    }

    def edit = {
        def ${propertyName} = ${className}.get(params.id)
        if (!${propertyName}) {
            flash.message = "\${message(code: 'default.not.found.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [${propertyName}: ${propertyName}]
        }
    }

    def update = {
        def ${propertyName} = ${className}.get(params.id)
        if (${propertyName}) {
            if (params.version) {
                def version = params.version.toLong()
                if (${propertyName}.version > version) {
                    <% def lowerCaseName = grails.util.GrailsNameUtils.getPropertyName(className) %>
                    ${propertyName}.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: '${domainClass.propertyName}.label', default: '${className}')] as Object[], "Another user has updated this ${className} while you were editing")
                    render(view: "edit", model: [${propertyName}: ${propertyName}])
                    return
                }
            }
            ${propertyName}.properties = params
            if (!${propertyName}.hasErrors() && ${propertyName}.save(flush: true)) {
                flash.message = "\${message(code: 'default.updated.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), ${propertyName}.id])}"
                redirect(action: "show", id: ${propertyName}.id)
            }
            else {
                render(view: "edit", model: [${propertyName}: ${propertyName}])
            }
        }
        else {
            flash.message = "\${message(code: 'default.not.found.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def ${propertyName} = ${className}.get(params.id)
        if (${propertyName}) {
            try {
                ${propertyName}.delete(flush: true)
                flash.message = "\${message(code: 'default.deleted.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "\${message(code: 'default.not.deleted.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "\${message(code: 'default.not.found.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), params.id])}"
            redirect(action: "list")
        }
    }

   def createInline = {
       def ${propertyName} = new ${className}()
       return [${propertyName}: ${propertyName}, "ownerController" : params.ownerController , "ownedProperty" : params.ownedProperty  , "ownerId":params.ownerId ]
   }

  def showInline = {
    def ${propertyName}
    if(params.pojoInstance){
        println params.pojoInstance
        ${propertyName} = params.pojoInstance
    }else{
       println params.id
       ${propertyName} = ${className}.get(params.id)
    }

       println ${propertyName}
       return [${propertyName}: ${propertyName},ownerId :params.ownerId ,ownedProperty:params.ownedProperty , ownerController : params.ownerController]
  }

   <% if(inlines){
    inlines.each{inlineName->
    p = domainClass.properties.find{ pr -> pr.name.equals(inlineName)}
  %>
  def add${inlineName} = {

     def  ${propertyName} =  ${className}.get(params.ownerId)
     def updateFlag  = false
     if( ${propertyName}){
        def ${p.referencedDomainClass?.shortName} ${inlineName}Instance
            if(params.id){
              updateFlag = true
              ${inlineName}Instance = ${p.referencedDomainClass?.shortName}.get(params.id)
              ${inlineName}Instance.properties = params


            } else{
             ${inlineName}Instance = new ${p.referencedDomainClass?.shortName}(params)
              ${propertyName}.addTo${org.apache.commons.lang.StringUtils.capitalize(inlineName)}(${inlineName}Instance)
            }


            if(${propertyName}.save(flush: true) && ${inlineName}Instance.save(flush: true)){

              redirect(action: "showInline", controller:"${p.referencedDomainClass?.propertyName}", params:[ownerId :params.ownerId , id:${inlineName}Instance.id , ownedProperty : "${inlineName}", ownerController : "${domainClass.shortName}" ])
            }

     }

  }


  <%
  }

  }

  def excludedProps = Event.allEvents.toList() << 'version'
  def props = domainClass.properties.findAll { !excludedProps.contains(it.name) && it.type != Set.class }
  props.each {  p ->
   if (p.isAssociation() || p.type == List.class) { %>

  def list${p.name} = {
        def ${propertyName} = ${className}.get(params.id)
        render("<ul>"+${propertyName}.${p.name}.each { "<li>"+ it  +"</li>"}+"</ul>")
    }


  <%
  }
}
  %>

   
}
